public class MyList {
    public ListNode head;

    static class ListNode{
        public int val;
        public ListNode next;
        public ListNode(int val){
            this.val = val;
        }

    }


    //寻找中间节点
    public ListNode middleNode() {
        ListNode fast = head;
        ListNode slow = head;
        while(fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;

    }

    public void addLast(int data) {
        ListNode cur = head;
        if(size() == 0){
            ListNode node = new ListNode(data);
            head = node;
        }
        while (cur != null){
            if(cur.next == null){
                ListNode node = new ListNode(data);
                cur.next = node;
                break;
            }
            cur = cur.next;
        }
    }

    public int size() {
        ListNode cur = head;
        int sum = 0;
        while (cur != null){
            sum++;
            cur = cur.next;
        }
        return sum;
    }

    public void display() {
        ListNode cur = head;
        while (cur != null){
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }

}
